import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from blue_objects.base_page import BasePage
from utils.log_utils import logger
from utils.web_util import click_exception

class TemperaturePage(BasePage):

    _BASE_URL = "https://weathershopper.pythonanywhere.com/"

    __TEMPERATURE_ELE = (By.ID, "temperature")
    __BTN_MOISTURIZER = (By.XPATH, "//*[contains(text(),'Buy moisturizers')]")
    __BTN_SUNSCREEN = (By.XPATH, "//*[contains(text(),'Buy sunscreens')]")

    """加载温度页面：定位温度"""

    def get_temperature(self):
        logger.info("获取页面温度信息：0~50")
        actural_temperature = self.do_find(self.__TEMPERATURE_ELE).text
        # time.sleep(1)
        tem_results = actural_temperature.split(" ")[0]
        int_temperature = int(tem_results)

        try:
            if isinstance(int_temperature, int):
                logger.info(f"本次刷新页面温度是：{int_temperature}")
                return int_temperature
        except ValueError as e:
            logger.info(f"报错信息是：{e}")


    def click_go_to_carts_moisturize(self):
        # ==》跳转到添加商品到购物车 moisturize页面
        from blue_objects.carts_moisturize_page import CartsMoisturizePage
        self.scroll_end()
        WebDriverWait(self.driver, 10).until(click_exception(*self.__BTN_MOISTURIZER))
        return CartsMoisturizePage(self.driver)

    def click_go_to_carts_sunscreen(self):
        # ==》跳转到添加商品到购物车 sunscreen页面
        from blue_objects.carts_sunscreen_page import CartsSunscreenPage
        self.scroll_end()
        WebDriverWait(self.driver, 10).until(click_exception(*self.__BTN_SUNSCREEN))
        return CartsSunscreenPage(self.driver)


